home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 109 / EnigmaAmiga109CD.iso / dalla rivista / host contacted / jikes.lha / jikes-1.11 / src / init.cpp < prev    next >
C/C++ Source or Header  |  1999-09-13  |  8KB  |  189 lines

  1. // $Id: init.cpp,v 1.7 1999/09/13 14:21:16 shields Exp $
  2. //
  3. // This software is subject to the terms of the IBM Jikes Compiler
  4. // License Agreement available at the following URL:
  5. // http://www.ibm.com/research/jikes.
  6. // Copyright (C) 1996, 1998, International Business Machines Corporation
  7. // and others.  All Rights Reserved.
  8. // You must accept the terms of that agreement to use this software.
  9. //
  10. #include "config.h"
  11. #include "semantic.h"
  12. #include "control.h"
  13.  
  14. void Semantic::ProcessVariableInitializer(AstVariableDeclarator *variable_declarator)
  15. {
  16.     if (! variable_declarator -> variable_initializer_opt)
  17.         return;
  18.  
  19.     VariableSymbol *symbol = variable_declarator -> symbol;
  20.  
  21.     AstArrayInitializer *array_initializer = variable_declarator -> variable_initializer_opt -> ArrayInitializerCast();
  22.     if (array_initializer)
  23.     {
  24.         //
  25.         // TODO: This code is not needed
  26.         //
  27.         // REMOVE:
  28.         //
  29.         // This operation may throw OutOfMemoryError
  30.         //
  31.         // SymbolSet *exception_set = TryExceptionTableStack().Top();
  32.         // if (exception_set)
  33.         // {
  34.         //     exception_set -> AddElement(control.RuntimeException());
  35.         //     exception_set -> AddElement(control.Error());
  36.         // }
  37.  
  38.         ProcessArrayInitializer(array_initializer, symbol -> Type());
  39.     }
  40.     else
  41.     {
  42.         AstExpression *init = (AstExpression *) variable_declarator -> variable_initializer_opt;
  43.         ProcessExpressionOrStringConstant(init);
  44.  
  45.         if (symbol -> Type() != init -> Type() && init -> Type() != control.no_type)
  46.         {
  47.             if (CanAssignmentConvert(symbol -> Type(), init))
  48.             {
  49.                 init = ConvertToType(init, symbol -> Type());
  50.                 variable_declarator -> variable_initializer_opt = init;
  51.             }
  52.             else if (init -> IsConstant() && control.IsSimpleIntegerValueType(init -> Type())
  53.                                           && control.IsSimpleIntegerValueType(symbol -> Type()))
  54.             {
  55.                 if (symbol -> Type() == control.byte_type)
  56.                      ReportSemError(SemanticError::INVALID_BYTE_VALUE,
  57.                                     init -> LeftToken(),
  58.                                     init -> RightToken());
  59.                 else if (symbol -> Type() == control.char_type)
  60.                      ReportSemError(SemanticError::INVALID_CHARACTER_VALUE,
  61.                                     init -> LeftToken(),
  62.                                     init -> RightToken());
  63.                 else ReportSemError(SemanticError::INVALID_SHORT_VALUE,
  64.                                     init -> LeftToken(),
  65.                                     init -> RightToken());
  66.             }
  67.             else
  68.             {
  69.                 ReportSemError(SemanticError::INCOMPATIBLE_TYPE_FOR_ASSIGNMENT,
  70.                                variable_declarator -> LeftToken(),
  71.                                init -> RightToken(),
  72.                                symbol -> Type() -> ContainingPackage() -> PackageName(),
  73.                                symbol -> Type() -> ExternalName(),
  74.                                init -> Type() -> ContainingPackage() -> PackageName(),
  75.                                init -> Type() -> ExternalName());
  76.             }
  77.         }
  78.  
  79.         if (symbol -> ACC_FINAL() && init -> IsConstant())
  80.             symbol -> initial_value = init -> value;
  81.     }
  82.  
  83.     return;
  84. }
  85.  
  86.  
  87. void Semantic::ProcessArrayInitializer(AstArrayInitializer *array_initializer, TypeSymbol *type)
  88. {
  89.     if (! type -> IsArray())
  90.     {
  91.         ReportSemError(SemanticError::INIT_SCALAR_WITH_ARRAY,
  92.                        array_initializer -> LeftToken(),
  93.                        array_initializer -> RightToken(),
  94.                        type -> Name());
  95.     }
  96.     else
  97.     {
  98.         for (int i = 0; i < array_initializer -> NumVariableInitializers(); i++)
  99.         {
  100.             AstArrayInitializer *sub_array_initializer = array_initializer -> VariableInitializer(i) -> ArrayInitializerCast();
  101.             TypeSymbol *array_subtype = type -> ArraySubtype();
  102.             if (sub_array_initializer)
  103.                  ProcessArrayInitializer(sub_array_initializer, array_subtype);
  104.             else
  105.             {
  106.                 AstExpression *init = (AstExpression *) array_initializer -> VariableInitializer(i);
  107.                 ProcessExpressionOrStringConstant(init);
  108.  
  109.                 if (array_subtype != init -> Type())
  110.                 {
  111.                     if (CanAssignmentConvert(array_subtype, init))
  112.                         array_initializer -> VariableInitializer(i) = ConvertToType(init, array_subtype);
  113.                     else if (array_subtype -> IsArray() && init -> Type() -> Primitive())
  114.                     {
  115.                         ReportSemError(SemanticError::INIT_ARRAY_WITH_SCALAR,
  116.                                        init -> LeftToken(),
  117.                                        init -> RightToken(),
  118.                                        array_subtype -> Name());
  119.                     }
  120.                     else if (init -> IsConstant() && control.IsSimpleIntegerValueType(init -> Type())
  121.                                                   && control.IsSimpleIntegerValueType(array_subtype))
  122.                     {
  123.                         if (array_subtype == control.byte_type)
  124.                              ReportSemError(SemanticError::INVALID_BYTE_VALUE,
  125.                                             init -> LeftToken(),
  126.                                             init -> RightToken());
  127.                         else if (array_subtype == control.char_type)
  128.                              ReportSemError(SemanticError::INVALID_CHARACTER_VALUE,
  129.                                             init -> LeftToken(),
  130.                                             init -> RightToken());
  131.                         else ReportSemError(SemanticError::INVALID_SHORT_VALUE,
  132.                                             init -> LeftToken(),
  133.                                             init -> RightToken());
  134.                     }
  135.                     else
  136.                     {
  137.                         ReportSemError(SemanticError::INCOMPATIBLE_TYPE_FOR_INITIALIZATION,
  138.                                        init -> LeftToken(),
  139.                                        init -> RightToken(),
  140.                                        array_subtype -> ContainingPackage() -> PackageName(),
  141.                                        array_subtype -> ExternalName(),
  142.                                        init -> Type() -> ContainingPackage() -> PackageName(),
  143.                                        init -> Type() -> ExternalName());
  144.                     }
  145.                 }
  146.             }
  147.         }
  148.     }
  149.  
  150.     return;
  151. }
  152.  
  153.  
  154. LiteralValue *Semantic::ComputeFinalValue(AstVariableDeclarator *variable_declarator)
  155. {
  156.     LiteralValue *value = NULL;
  157.  
  158.     VariableSymbol *variable = variable_declarator -> symbol;
  159.     TypeSymbol *type = (TypeSymbol *) variable -> owner;
  160.  
  161.     state_stack.Push(type -> semantic_environment);
  162.     if (! error)
  163.         error = new SemanticError(control, source_file_symbol);
  164.     error -> EnteringClone();
  165.     variable_declarator -> pending = true;
  166.  
  167.     AstExpression *init_expr = (AstExpression *) variable_declarator -> variable_initializer_opt;
  168.     AstExpression *init_clone = (AstExpression *) init_expr -> Clone(compilation_unit -> ast_pool);
  169.     ProcessExpressionOrStringConstant(init_clone);
  170.     if (variable -> Type() != init_clone -> Type() && init_clone -> Type() != control.no_type)
  171.     {
  172.         if (CanAssignmentConvert(variable -> Type(), init_clone))
  173.              init_clone = ConvertToType(init_clone, variable -> Type());
  174.         else init_clone -> value = NULL;
  175.     }
  176.     value = init_clone -> value;
  177.  
  178. //
  179. // STG:
  180. //        delete init_clone; // destroy the clone
  181. //
  182.  
  183.     variable_declarator -> pending = false;
  184.     error -> ExitingClone();
  185.     state_stack.Pop();
  186.  
  187.     return value;
  188. }
  189.